home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / glass / glass.lha / GLASS / libtmc / fscstr.c < prev    next >
C/C++ Source or Header  |  1990-11-06  |  2KB  |  105 lines

  1. /* 
  2.    Copyright (C) 1990 C van Reewijk, email: dutentb.uucp!reeuwijk
  3.  
  4. This file is part of GLASS.
  5.  
  6. GLASS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GLASS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GLASS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* File: fscstr.c
  21.  * 
  22.  * Handle 'fscan_<type>' for type 'string'.
  23.  */
  24.  
  25. /* Standard libraries */
  26. #include <ctype.h>
  27. #include <stdio.h>
  28.  
  29. /* Header of the library this code is part of. */
  30. #include "tmc.h"
  31. #include "config.h"
  32.  
  33. #define TRUE 1
  34. #define FALSE 0
  35. typedef short int bool;
  36.  
  37. /* Try to read a string in the buffer 'buf'. Give an error
  38.    message if this is not successful. A string may contain
  39.    escape sequences with a '\', but no newlines. The '"'
  40.    around the string are stripped.
  41.  */
  42. int fscan_string( f, s )
  43.  FILE *f;
  44.  char **s;
  45. {
  46.     register int c;
  47.     register bool done;
  48.     char buf[STRBUFSZ];
  49.     register char *bufp;
  50.     register int brac;
  51.  
  52.     *s = (char *)0;
  53.     bufp = buf;
  54.     brac = fscanopenbrac( f );
  55.     c = fgetc( f );
  56.     if( c != '"' ){
  57.     (void) strcpy( tmerrmsg, "string expected" );
  58.     return( 1 );
  59.     }
  60.     done = FALSE;
  61.     while( !done ){
  62.     c = fgetc( f );
  63.     if( c == '\n' ){
  64.         (void) strcpy( tmerrmsg, "newline in string" );
  65.         return( 1 );
  66.     }
  67.     if( c == '"' ) done = TRUE;
  68.     if( c == '\\' ){
  69.         c = fgetc( f );
  70.         switch( c ){
  71.         case 'b': c = '\b'; break;
  72.         case 'f': c = '\f'; break;
  73.         case 'n': c = '\n'; break;
  74.         case 'r': c = '\r'; break;
  75.         case 't': c = '\t'; break;
  76.         case 'v': c = '\v'; break;
  77.         default:
  78.             if( isdigit( c ) ){
  79.             int val;
  80.  
  81.             val = c-'0';
  82.             c = fgetc( f );
  83.             if( isdigit( c ) ){
  84.                 val = val*8 + (c-'0');
  85.                 c = fgetc( f );
  86.                 if( isdigit( c ) ){
  87.                 val = val*8 + (c-'0');
  88.                 }
  89.                 else
  90.                 ungetc( c, f );
  91.             }
  92.             else
  93.                 ungetc( c, f );
  94.             c = val;
  95.             }
  96.             break;
  97.         }
  98.     }
  99.     if( !done ) *bufp++ = c;
  100.     }
  101.     *bufp = '\0';
  102.     *s = new_string( buf );
  103.     return( fscanclosebrac( f, brac ) );
  104. }
  105.